package controller

import (
	"context"

	"net/http"
	"path"
	"web/api"
	"web/global"
	"web/proto/myhouse"
	"web/utils"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

//获取已发布房源信息  假数据
func GetUserHouses(ctx *gin.Context) {
	var req myhouse.GetReq
	var res myhouse.GetResp
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)

	//获取用户名
	userName := sessions.Default(ctx).Get("userName")
	req.UserName = userName.(string)
	h := api.House{}
	h.GetHouseInfo(context.Background(), &req, &res)

	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data

}

type HouseStu struct {
	Acreage   string   `json:"acreage"`
	Address   string   `json:"address"`
	AreaId    string   `json:"area_id"`
	Beds      string   `json:"beds"`
	Capacity  string   `json:"capacity"`
	Deposit   string   `json:"deposit"`
	Facility  []string `json:"facility"`
	MaxDays   string   `json:"max_days"`
	MinDays   string   `json:"min_days"`
	Price     string   `json:"price"`
	RoomCount string   `json:"room_count"`
	Title     string   `json:"title"`
	Unit      string   `json:"unit"`
}

//发布房源
func PostHouses(ctx *gin.Context) {
	//获取数据   bind数据的时候不带自动转换   c.getInt()
	var house HouseStu

	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	err := ctx.Bind(&house)

	//校验数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "获取数据错误:", err)
		return
	}

	//获取用户名
	userName := sessions.Default(ctx).Get("userName")
	var res myhouse.Response
	var req myhouse.Request = myhouse.Request{
		Acreage:   house.Acreage,
		Address:   house.Address,
		AreaId:    house.AreaId,
		Beds:      house.Beds,
		Capacity:  house.Capacity,
		Deposit:   house.Deposit,
		Facility:  house.Facility,
		MaxDays:   house.MaxDays,
		MinDays:   house.MinDays,
		Price:     house.Price,
		RoomCount: house.RoomCount,
		Title:     house.Title,
		Unit:      house.Unit,
		UserName:  userName.(string),
	}
	req.UserName = userName.(string)
	h := api.House{}
	h.PubHouse(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data
}

//上传房屋图片
func PostHousesImage(ctx *gin.Context) {

	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)

	//获取数据
	houseId := ctx.Param("id")
	fileHeader, err := ctx.FormFile("house_image")
	//校验数据
	if houseId == "" || err != nil {
		global.GlobalLog.Error(context.Background(), "传入数据不完整:", err)
		resp["errno"] = utils.RECODE_DATAERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DATAERR)
		return
	}

	//三种校验 大小,类型,防止重名  fastdfs
	if fileHeader.Size > 50000000 {
		global.GlobalLog.Error(context.Background(), "文件过大,请重新选择")
		resp["errno"] = utils.RECODE_DATAERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DATAERR)
		return
	}

	fileExt := path.Ext(fileHeader.Filename)
	if fileExt != ".png" && fileExt != ".jpg" && fileExt != ".jpeg" {
		global.GlobalLog.Error(context.Background(), "文件类型错误,请重新选择")
		resp["errno"] = utils.RECODE_DATAERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DATAERR)
		return
	}

	//获取文件字节切片
	file, _ := fileHeader.Open()
	buf := make([]byte, fileHeader.Size)
	file.Read(buf)

	h := api.House{}
	var req myhouse.ImgReq = myhouse.ImgReq{
		HouseId: houseId,
		ImgData: buf,
		FileExt: fileExt,
	}
	var res myhouse.ImgResp
	h.UploadHouseImg(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data
}

//获取房屋详情
func GetHouseInfo(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取数据
	houseId := ctx.Param("id")
	//校验数据
	if houseId == "" {
		global.GlobalLog.Error(context.Background(), "获取数据错误")
		return
	}
	userName := sessions.Default(ctx).Get("userName")
	var req myhouse.DetailReq = myhouse.DetailReq{
		HouseId:  houseId,
		UserName: userName.(string),
	}
	var res myhouse.DetailResp
	h := api.House{}
	h.GetHouseDetail(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data
}

func GetIndex(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	var req myhouse.IndexReq = myhouse.IndexReq{}
	var res myhouse.GetResp
	h := api.House{}
	h.GetIndexHouse(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data
}

//搜索房屋
func GetHouses(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取数据
	//areaId
	aid := ctx.Query("aid")
	//start day
	sd := ctx.Query("sd")
	//end day
	ed := ctx.Query("ed")
	//排序方式
	sk := ctx.Query("sk")
	//page  第几页
	//ctx.Query("p")
	//校验数据
	if aid == "" || sd == "" || ed == "" || sk == "" {
		global.GlobalLog.Error(context.Background(), "传入数据不完整")
		resp["errno"] = utils.RECODE_DATAERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DATAERR)
		return
	}

	var req myhouse.SearchReq = myhouse.SearchReq{
		Aid: aid,
		Sd:  sd,
		Ed:  ed,
		Sk:  sk,
	}
	var res myhouse.GetResp
	h := api.House{}
	h.SearchHouse(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data

}
